package com.wx.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wx.common.JsonArrayResult;
import com.wx.common.JsonObjectResult;
import com.wx.common.redis.RedisCacheHand;
import com.wx.common.redis.RedisUtil;
import com.wx.entity.GoodsContent;
import com.wx.entity.GoodsContentPics;
import com.wx.entity.GoodsProperties;
import com.wx.entity.IndexCategory;
import com.wx.entity.IndexGoods;
import com.wx.entity.goods.Reputation;
import com.wx.service.GoodsService;

@RestController
@RequestMapping("/api/goods")
public class GoodsController {

	@Autowired
	private RedisUtil redis;
	
	@Autowired
	private GoodsService goodsService;

	// 请求商品详情
	@GetMapping("/goods_detail")
	@ResponseBody
	public JsonObjectResult get_goods_detail(@RequestParam(value = "id", required = false) Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();

		IndexGoods goods = new IndexGoods();
		IndexCategory category = new IndexCategory();
		GoodsContent content = new GoodsContent();
		List<GoodsContentPics> contentPics = new ArrayList<GoodsContentPics>();
		List<GoodsProperties> propertiesFather = new ArrayList<GoodsProperties>();
		List<GoodsProperties> propertiesChild = new ArrayList<GoodsProperties>();
		GoodsProperties deal = null;

		goods = goodsService.selectByPrimaryKey(id);
		if (goods != null) {
			category = goodsService.selectByCategoryId(goods.getCategoryId());
			content = goodsService.selectContentByGoodsId(id);
			contentPics = goodsService.selectContentPicsByGoodsId(id);
		}

		String property_ids = goods.getPropertyIds();
		if(!"".equals(property_ids) && property_ids != null) {
			List<String> property_list = Arrays.asList(property_ids.split(","));
			for(String item : property_list) {
				deal = new GoodsProperties();
				deal = JSON.parseObject(redis.get(RedisCacheHand.GoodsPropertiesCache + item), GoodsProperties.class);
				propertiesFather.add(deal);
			}
			
			for(int i = 0; i < propertiesFather.size(); i++) {
				List<String> property_child_list = Arrays.asList(redis.get(RedisCacheHand.GoodsPropertyCache + goods.getId() + propertiesFather.get(i).getId()).split(","));
				for(String item_child : property_child_list) {
					propertiesChild.add(JSON.parseObject(redis.get(RedisCacheHand.GoodsPropertiesCache + item_child), GoodsProperties.class));
				}
				propertiesFather.get(i).setChildsCurGoods(propertiesChild);
				propertiesChild = null;
				property_child_list = null;
			}
		}
		
		map.put("basicInfo", goods);
		map.put("category", category);
		map.put("content", content.getContent());
		map.put("extJson", null);
		map.put("pics", contentPics);
		map.put("properties", propertiesFather);

		JsonObjectResult result = new JsonObjectResult(JSONObject.parseObject(JSON.toJSONString(map)));
		return result;
	}

	/**
	 * 请求商品评价
	 * @param id 商品id
	 * @return
	 */
	@PostMapping("/goods_reputation")
	@ResponseBody
	public JsonArrayResult get_goods_by_recommendStatus(@RequestParam(value = "goodsId", required = false) Integer id) {
		List<Reputation> list = new ArrayList<Reputation>();
		list = goodsService.selectGoodsReputation(id);
		JsonArrayResult result = new JsonArrayResult(JSONArray.parseArray(JSON.toJSONString(list)));
		return result;
	}

}
